package com.zzw.golableex;

import com.zzw.common.ApiCommonResponse;
import com.zzw.common.exception.CommonException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 */
@Configuration
@ConditionalOnProperty(prefix = "com.zzw.golable.ex", name = "enable")
public class GlobalExceptionConfiguration {

    @ControllerAdvice
    @ResponseBody
    public static class GlobalExceptionHandler {
        @ExceptionHandler(value = CommonException.class)
        public ApiCommonResponse<?> commonExceptionHandler(CommonException e) {
            return ApiCommonResponse.of("", e.status, e.message);
        }

        @ExceptionHandler(value = Exception.class)
        public ApiCommonResponse<?> ExceptionHandler(Exception e) {
            return ApiCommonResponse.of("", "-1", e.getMessage());
        }
    }

}
